/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package mnstudent.dataacesslayer;

import mnstudent.datatranferobject.StaffDTO;
import java.sql.ResultSet;

/**
 *
 * @author 11520_000
 */
public class StaffDAL {
       
    //<editor-fold defaultstate="collapsed" desc="Attributes">
    private StaffDTO m_StaffDTO;
    private DataExecuteUseStoreProcedure m_DataExecute;
    private String  m_sqlCmd;
    
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="Constuctor">
    public StaffDAL(){
        this.m_StaffDTO = new StaffDTO();
        this.m_DataExecute = new DataExecuteUseStoreProcedure();    
        this.m_sqlCmd = "";
    }
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="Method">
    
     /**
     * Ham lay tat ca cac Nhan vien
     *
     * @return ResultSet
     */
    public ResultSet getAllStaff(){
        try
        {
            m_sqlCmd = "{Call GetAllStaffFromDatabase}";
            ResultSet _rsData = this.m_DataExecute.getDataFromDatabase(m_sqlCmd,null);
            return _rsData;
        }catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return null;
    }
    
    /**
     * Ham lay khoa theo ma khoa
     * @param _id
     * @return tra ve table chon
     */
    public ResultSet getStaffById(String _id)
    {
        try {
            m_sqlCmd = "{Call GetStaffByIdFromDatabase(?)}";
            String[] arrId = {_id};
            return this.m_DataExecute.getDataFromDatabase(m_sqlCmd, arrId);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return null;
    }
    
    public ResultSet getStaffByUserNameAndPasswordFromDatabase(String _userName, String _passWord)
    {
       try {
            m_sqlCmd = "{Call GetEmployeeByUserPasswordFromDatabase(?,?)}";
            String[] arrId = {_userName, _passWord};
            return this.m_DataExecute.getDataFromDatabase(m_sqlCmd, arrId);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return null; 
    }
    
    
     /**
     * ham them moi khoa xuong database
     * @param _staff
     * @return true: success, false: fail
     */
    public boolean insertStaffToDatabase(StaffDTO _staff)
    {
       try {
           m_sqlCmd = "{Call InsertStaffToDatabase(?,?,?,?,?,?,?,?,?)}";
           Object[] arrStr = {_staff.getStaffID(), _staff.getFullName(),
               _staff.getGender(), _staff.getDateOfBirth(), _staff.getPhoneNumber(), 
               _staff.getAddress(), _staff.getPositionID(), _staff.getUserName(), _staff.getPassword()};

           return this.m_DataExecute.updateDataToDatabase(m_sqlCmd,arrStr)>0;
           
       } catch (Exception ex) {
          ex.printStackTrace();
       }
        return false;
    }
    
    /**
     * Ham cap nhat sua doi thong tin khoa
     * @param _staff
     * @return tue: thanh cong, false: that bai
     */
    public boolean updateStaffToDatabase(StaffDTO _staff)
    {
       try {
           m_sqlCmd = "{Call UpdateStaffToDatabase(?,?,?,?,?,?,?,?)}";
           Object[] arrStr = {_staff.getStaffID(), _staff.getFullName(),
               _staff.getGender(), _staff.getDateOfBirth(), _staff.getPhoneNumber(), 
               _staff.getAddress(), _staff.getPositionID(), _staff.getPassword()};

           return this.m_DataExecute.updateDataToDatabase(m_sqlCmd,arrStr)>0;
           
       } catch (Exception ex) {
          ex.printStackTrace();
       }
        return false;
    }
    
    /**
     * Ham cap nhat sua doi thong tin khoa
     * @param _staff
     * @return tue: thanh cong, false: that bai
     */
    public boolean updateStaffNoPasswordToDatabase(StaffDTO _staff)
    {
       try {
           m_sqlCmd = "{Call UpdateStaffNoPasswordToDatabase(?,?,?,?,?,?,?)}";
           Object[] arrStr = {_staff.getStaffID(), _staff.getFullName(),
               _staff.getGender(), _staff.getDateOfBirth(), _staff.getPhoneNumber(), 
               _staff.getAddress(), _staff.getPositionID()};

           return this.m_DataExecute.updateDataToDatabase(m_sqlCmd,arrStr)>0;
           
       } catch (Exception ex) {
          ex.printStackTrace();
       }
        return false;
    }
    /**
     * Ham cap nhat sua doi thong tin khoa
     * @param _staff
     * @return tue: thanh cong, false: that bai
     */
    public boolean updatePasswordOfStaffToDatabase(String _idStaff, String _newPassword)
    {
       try {
           m_sqlCmd = "{Call UpdatePasswordOfStaffToDatabase(?,?)}";
           String[] arrStr = {_idStaff, _newPassword};

           return this.m_DataExecute.updateDataToDatabase(m_sqlCmd,arrStr)>0;
           
       } catch (Exception ex) {
          ex.printStackTrace();
       }
        return false;
    }
    
            
    /**
     * Ham xoa mot khoa ra khoi database
     * @param _staff
     * @return 
     */
     public boolean deleteStaffFromDatabase(StaffDTO _staff)
    {
        try {
           m_sqlCmd = "{Call DeleteStaffFromDatabase(?)}";
           String[] arrStr = {_staff.getStaffID()};

           return this.m_DataExecute.updateDataToDatabase(m_sqlCmd, arrStr)>0;
           
       } catch (Exception ex) {
          ex.printStackTrace();
       }
        return false;
    }
    //</editor-fold>
}
